package com.solution.service;

import java.util.Iterator;
import java.util.List;

import javax.ejb.EJBException;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

import org.apache.log4j.Logger;
import org.jboss.annotation.ejb.RemoteBinding;

import com.solution.constant.ErrorMessages;
import com.solution.dao.DAOFactory;
import com.solution.dao.SystemVariableDAO;
import com.solution.exception.DAOException;
import com.solution.exception.ServiceBeanException;
import com.solution.model.SystemVariable;
import com.solution.util.SearchVO;

/**
 * This class exposes all the services for creation, update, read and
 * deletion of SystemVariable.
 *
 *
 */

@Stateless
@Remote(SystemVariableService.class)
@RemoteBinding(jndiBinding="SystemVariableService")
@TransactionManagement(TransactionManagementType.CONTAINER)
public class SystemVariableServiceImpl implements SystemVariableService{
	
	/**
	 * DAO instance.
	 */
	private SystemVariableDAO systemVariableDao = (SystemVariableDAO)DAOFactory.getDAOInstance(SystemVariableDAO.class.getName());
	
	/**
	 * Logger instance.
	 */
	private Logger log = Logger.getLogger(SystemVariableServiceImpl.class.getName());
	/**
	 * This service creates the SystemVariable entry in the database after performing
	 * required business logic.
	 * @param systemVariable
	 * @return
	 * @throws ServiceBeanException
	 */
	public SystemVariable create(SystemVariable systemVariable) throws ServiceBeanException {
		log.debug("Entering :: SystemVariableServiceImpl:create()");
		try{
			// Create the entry in database.
			systemVariable = systemVariableDao.create(systemVariable);
			log.info("The new SystemVariable created with ID : "+systemVariable.getId());
			log.debug("Exiting :: SystemVariableServiceImpl:create()");
			return systemVariable;
		}
		catch(DAOException ex){
			log.error(ErrorMessages.SYSTEMVARIABLE_CREATE_EXCEPTION, ex);
			throw new EJBException(ex);
		}
	}
	/**
	 * This service creates the SystemVariable entry 
	 * @param systemVariable
	 * @return
	 * @throws ServiceBeanException
	 */
	
	public SystemVariable update(SystemVariable systemVariable) throws ServiceBeanException {
		log.debug("Entering :: SystemVariableServiceImpl:update()");
		try{			
			systemVariable = systemVariableDao.update(systemVariable);
			log.info("The  SystemVariable updated with ID : "+systemVariable.getId());
			log.debug("Exiting :: SystemVariableServiceImpl:update()");
			return systemVariable;
		}
		catch(DAOException ex){
			log.error(ErrorMessages.SYSTEMVARIABLE_UPDATE_EXCEPTION, ex);
			throw new EJBException(ex);
		}
	}
	
	/**
	 * Updates list of system variables
	 * @param systemVariableList
	 * @return
	 * @throws ServiceBeanException
	 */
	public int updateSystemVariables(List<SystemVariable> systemVariableList) throws ServiceBeanException {
		log.debug("Entering :: SystemVariableServiceImpl:updateSystemVariables()");
		SystemVariable systemVariable = null;
		try{	
			Iterator<SystemVariable> iter = systemVariableList.iterator();
			int count = 0;
			while(iter.hasNext())
			{
				systemVariable = iter.next();
				systemVariable = systemVariableDao.update(systemVariable);
				log.info("The  SystemVariable updated with ID : "+systemVariable.getId());
				count ++;
			}
			
			log.debug("Exiting :: SystemVariableServiceImpl:updateSystemVariables()");
			return count;
		}
		catch(DAOException ex){
			log.error(ErrorMessages.SYSTEMVARIABLE_UPDATE_EXCEPTION, ex);
			throw new EJBException(ex);
		}
	}
	
	/**
	 * This service reads a SystemVariable from database based on the primary
	 * key.
	 * @param sysVariableName
	 * @return
	 * @throws ServiceBeanException
	 */
	public SystemVariable getValue(String sysVariableName, Long clientId) throws ServiceBeanException {
		log.debug("Entering :: SystemVariableServiceImpl:read()");
		try{
			SearchVO searchVO = new SearchVO();
			searchVO.addEqualsCondition("variableName", sysVariableName);
			searchVO.addEqualsCondition("clientType.id", clientId, true);
			SystemVariable systemVariable = systemVariableDao.searchUnique(searchVO);		
			log.debug("Exiting :: SystemVariableServiceImpl:read()");
			return systemVariable;
		}
		catch(DAOException ex){
			log.error(ErrorMessages.SYSTEMVARIABLE_READ_EXCEPTION, ex);
			throw new EJBException(ex);
		}
	}
	
	/**
	 * This service fetches a list of SystemVariable based on a search criteria which can
	 * be set using various parameters of the systemVariable object.
	 * @param product
	 * @return
	 * @throws ServiceBeanException
	 */
	public List<SystemVariable> get(SearchVO searchVO) throws ServiceBeanException {
		log.debug("Entering :: SystemVariableServiceImpl:get()");
		try{			
			log.debug("Exiting :: UserDAOImpl:getSystemVariableList()");
			return systemVariableDao.search(searchVO);			
		}
		catch(DAOException ex){
			log.error(ErrorMessages.SYSTEMVARIABLE_READ_EXCEPTION, ex);
			throw new EJBException(ex);
		}
	}
}